package itheima.Healthy;

import java.text.DecimalFormat;
import java.util.Scanner;

public class HuaiYaHealthyPlus {
    public static void main(String[] args) {
        // 1.录入用户信息
        // 1.1.创建Scanner对象
        Scanner scanner = new Scanner(System.in);
        // 1.2 录入升高
        short height = (short) readValue( "升高（cm）",140,220);

        // 1.3 录入体重
        double weight = readValue("体重（kg）",40,200);

        // 1.4 录入年龄
        byte age = (byte) readValue("年龄（岁）",18,70);
        // 1.5 录入性别
        byte sex = (byte) readValue("性别（0：男/1：女）",0,1);

        // 1.6 录入活动级别
        byte activityLevel = (byte) readValue("活动级别（0：久坐/1：轻度/2：中度/3：高度）",0,3);


        // 2.计算MBMI，并且判断体型，设定运动目标
        // 2.1计算BMI
        double bmi = caltulateBmi(height, weight);
        // 2.2判断体型
        String bodyType = caltulateString(bmi);
        // 2.3设定运动目标
        String goal = setGoal(bodyType);


        // 3.计算每日摄入热量建议，包括BMR、TDEE、每日热量
        // 3.1 计算BMR 每日基础代谢率
        double bmr = caltulateBmr(sex, weight, height, age);
        // 3.2计算tdee 总能量消耗
        double tdee = caltulateTdee(activityLevel, bmr);
        // 3.3计算每日摄入热量
        double dailyCalories = calculateDailyCalories(goal, tdee);


        // 4.计算每日摄入的营养素的质量
        double protein = calculatProtein(dailyCalories);
        double fat = calculatFat(dailyCalories);
        double carbohydrate = calculatCarbohydrate(dailyCalories);


        // 5.打印结果
        printResult(bmi, bodyType, goal, bmi, tdee, dailyCalories, protein, fat, carbohydrate);
    }

    public static double readValue(String prompt,double min,double max){
        Scanner scanner = new Scanner(System.in);
        double value;
        while (true){
            System.out.print("请输入" + prompt + "：");
            value =  scanner.nextDouble();
            if (value >= min && value <= max){
                System.out.println("输入成功");
                break;
            };
            System.out.println("输入错误，请重新输入一个" + min + "-" + max + "之间的值！");
        }
        return value;
    }
    public static double caltulateBmi(short height, double weight) {
        double heightINMeters =  height / 100.0;
        return weight / (heightINMeters * heightINMeters);
    }

    public static String caltulateString(double bmi) {
        String bodyType = "";
        if (bmi < 18.5) {
            bodyType = "偏瘦";
        } else if (bmi >= 18.5 && bmi < 24) {
            bodyType = "正常";
        } else if (bmi >= 24 && bmi < 28) {
            bodyType = "超重";
        } else {
            bodyType = "肥胖";
        }
        return bodyType;
    }

    public static String setGoal(String bodyType) {
        return switch (bodyType) {
            case "偏瘦" -> "增肌";
            case "超重", "肥胖" -> "减重";
            default -> "保持";
        };
    }

    public static double caltulateBmr(byte sex, double weight, short height, byte age) {
        return sex == 0 ?
                88.362 + (13.397 * weight) + (4.799 * height) - (5.677 * age) :
                447.593 + (9.247 * weight) + (3.098 * height) - (4.330 * age);
    }

    public static double caltulateTdee(byte activityLevel, double bmr) {
        double activityFactor = switch (activityLevel){
            case 0 -> 1.2;
            case 1 -> 1.375;
            case 2 -> 1.55;
            default -> 1.725;
        };
        return bmr * activityFactor;
    }

    public static double calculateDailyCalories(String goal, double tdee) {
        return switch (goal) {
            case "增肌" -> tdee + 300;
            case "减重" -> tdee - 300;
            default -> tdee;
        };
    }

    public static double calculatProtein(double dailyCalories) {
        final  double PROTEIN_PERCENT = 0.3;
        final int PROTEIN_KCAL_PER_PROTEIN = 4;
        return dailyCalories * PROTEIN_PERCENT / PROTEIN_KCAL_PER_PROTEIN;
    }

    public static double calculatFat(double dailyCalories) {
        final double FAT_PERCENT = 0.2;
        final  int FAT_KCAL_PER_FAT = 9;
        return dailyCalories * FAT_PERCENT / FAT_KCAL_PER_FAT;
    }

    public static double calculatCarbohydrate(double dailyCalories) {
        final double CARB_PERCENT = 0.5;
        final int CARB_KCAL_PER_CARB = 4;
        return dailyCalories * CARB_PERCENT / CARB_KCAL_PER_CARB ;
    }


    public static void printResult(
        double bmi, String bodyType, String goal,
        double bmr, double tdee, double dailyCalories,
        double protein, double fat, double carbohydrate) {
    DecimalFormat df = new DecimalFormat("0.0");
    System.out.println("您的BMI是：" + df.format(bmi) + "，体型是：" + bodyType);
    System.out.println("运动目标：" + goal + "，加油！");
    System.out.println("============饮食计划============");
    System.out.println("您的每日基础代谢为：" + (int) bmr + "kcal,结合运动后的总代谢：" + (int) tdee + "kcal");// 输出强转
    System.out.println("结合您的运动目标，建议你每日摄入的能量为：" + (int) dailyCalories + "kcal，其中：");
    System.out.println("蛋白质：" + (int) protein + "克");
    System.out.println("脂肪：" + (int) fat + "克");
    System.out.println("碳水化合物：" + (int) carbohydrate + "克");
    System.out.println("以上是健康饮食建议，祝您生活愉快！");
    System.out.println("===============================");
    }
}